<?php defined('BASEPATH') OR die('No direct access allowed.');
/**
 * Description of "Branches_model"
 *
 * Created on Dec 20, 2010
 * @author Ramon Gutierrez
 * @copyright Bitstop Network Services Inc.
 * @version 0.1
 */

class Branches_model extends Model {
	public function Branches_model() {
        parent::Model();
		$this->CI =& get_instance();
    }
	
	// Get all items
	public function getAll($client_id = false) {
		$query = $this->db->query("SELECT id, account_id, name, street_address, city, state, country_id, zip, contact_number FROM branches " . ($client_id ? "WHERE client_id = $client_id" : "") . " ORDER BY post_date DESC");
		return $query;
	}
	
	// Get company name of branch
	public function getCompany($id) {
		$query = $this->db->query("SELECT c.name AS company FROM clients c, branches b WHERE b.id = " . $id . " AND c.id = b.client_id LIMIT 1");
		$row = $query->row();
		return $row->company;
	}
	
	// Get all branches of company
	public function getBranchesByClient($client_id, $fields = "id, name") {
		$query = $this->db->select($fields)->where('client_id', $client_id)->get('branches');
		if($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}
	
	// Get branch based on account
	public function getBranchNameByAcct($account_id) {
		$query = $this->db->select('name')->where('account_id', $account_id)->get('branches');
		if($query->num_rows() > 0) {
			$row = $query->row();
			return $row->name;
		} else {
			return false;
		}
	}
}

/* End of file branches_model.php */
/* Location: ./system/application/models/branches_model.php */